<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card class="tree-card">
        <!-- 用了一个行列布局 -->
        <treeTools :tree-node="company" :is-root="true" @addDepts="addDepts" />
        <el-tree :data="departs" :props="defaultProps" :default-expand-all="true">
          <!-- 传入内容 插槽内容 会循环多次 有多少节点 就循环多少次 -->
          <!-- 作用域插槽 slot-scope="obj" 接收传递给插槽的数据   data 每个节点的数据对象-->
          <treeTools slot-scope="{ data }" :tree-node="data" @delDepts="getDePartMents" @addDepts="addDepts" @editDepts="editDepts" />
        </el-tree>
      </el-card>

    </div>
    <addDept ref="addDept" :show-dialog.sync="showDialog" :tree-node="Node" @addDepts="getDePartMents" />
  </div>
</template>

<script>
import treeTools from '@/views/departments/components/tree-tools.vue'
import addDept from '@/views/departments/components/add-dept.vue'
import { getDePartMents } from '@/api/departments'
import { transListToTreeData } from '@/utils'
export default {
  components: {
    treeTools,
    addDept
  },
  data() {
    return {
      company: { },
      departs: [],
      defaultProps: {
        label: 'name' // 表示 从这个属性显示内容
      },
      showDialog: false,
      Node: null
    }
  },
  created() {
    this.getDePartMents()
  },
  methods: {
    async getDePartMents() {
      const result = await getDePartMents()
      console.log(result)
      this.company = { name: result.companyName, manager: '负责人', id: '' }
      this.departs = transListToTreeData(result.depts, '')
    },
    addDepts(Node) {
      this.showDialog = true

      this.Node = Node
    },
    // 编辑部门节点
    editDepts(Node) {
      // 首先打开弹层
      this.showDialog = true
      this.Node = Node // 赋值操作的节点
      this.$refs.addDept.getDepartDetail(Node.id) // 直接调用子组件中的方法 传入一个id
    }
  }
}
</script>

<style scoped lang="scss">
.tree-card {
  padding: 30px  140px;
  font-size:14px;
}
</style>
